package org.lai.controller;

import lombok.extern.slf4j.Slf4j;
import org.lai.pojo.*;
import org.lai.service.ClazzsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzsController {
    @Autowired
    private ClazzsService clazzsService;

    /**
     * 获取所有班级和班级的班主任名称
     * 条件分页查询(PageHelper)
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam) {
        log.info("查询所有班级:{}",clazzQueryParam);
        PageResult<Clazz> pageResult = clazzsService.list(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增班级信息,接收一个json类型的数据需要使用@RequestBody主键解析
     * @param clazz
     * @return
     */
    @PostMapping
    public Result addClazzs(@RequestBody Clazz clazz){
        log.info("添加班级:{}",clazz);
        clazzsService.addClazzs(clazz);
        return Result.success();
    }
    /**
     * 数据回显
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询班级信息:{}",id);
        Clazz result = clazzsService.getInfo(id);
        return Result.success(result);
    }
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息:{}",clazz);
        clazzsService.update(clazz);
        return Result.success();
    }
    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级:{}",id);
        clazzsService.delete(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result clazzslist(){
        List<Clazz> list = clazzsService.getList();
        return Result.success(list);
    }
}
